using System;
using System.Text;
using System.Collections.Generic;

namespace Axe.Logging
{
    [Flags]
    [Serializable]
    public enum LogEntryType
    {
        Debug         = 1,
        Info          = 2,
        Warning       = 4,
        Error         = 8,
        Alert         = 16,
        Exception     = 32
    };

    /// <summary>
    /// This class implements container for storing log events.
    /// </summary>
    public class LogEntry
    {
        LogEntryType m_EventType = LogEntryType.Info;
        DateTime m_EventTime = DateTime.Now;

        Dictionary<string, string> m_Items = new Dictionary<string, string>();

        public string this[string itemKey]
        {
            get
            {
                return m_Items[itemKey];
            }
            set
            {
                m_Items[itemKey] = value;
            }
        }

        public DateTime EventTime
        {
            get { return m_EventTime; }
            set { m_EventTime = value; }
        }

        public LogEntryType EventType
        {
            get { return m_EventType; }
            set { m_EventType = value; }
        }

        public string Text
        {
            get
            {
                return this["Text"];
            }
            set
            {
                this["Text"] = value;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LogEntry"/> class.
        /// </summary>
        /// <param name="e">The e.</param>
        public LogEntry(Exception e)
        {
            EventType = LogEntryType.Exception;

            this["Message"]    = e.Message;
            this["Source"]     = e.Source;
            this["StackTrace"] = e.StackTrace;
            this["TargetSite"] = e.TargetSite.Name;
        }

        public LogEntry(LogEntryType type,DateTime time)
            : this(type, time,"")
        {
        }

        public LogEntry(LogEntryType type, DateTime time, string text)
        {
            EventType = type;
            EventTime = time;
            Text = text;
        }

        public LogEntry(LogEntryType type, string text)
            : this(type, DateTime.Now, text)
        {
        }

        public static explicit operator LogEntry(System.Exception e)
        {
            return new LogEntry(e);
        }

        #region public override string ToString()
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            string eventHeader = String.Format("[{0}] {1}", EventType.ToString(), EventTime.ToString("G"));
            sb.AppendLine(eventHeader);

            foreach (string item in m_Items.Keys)
            {
                string value = m_Items[item];

                if (value != null && value != "")
                {
                    sb.AppendLine(String.Format("{0} :{1}", item, value));
                }
            }

            return sb.ToString();
        } 
        #endregion
    }
}